library(ggplot2)
library(plotly)
## 
## Attaching package: 'plotly'
## The following object is masked from 'package:ggplot2':
## 
##     last_plot
## The following object is masked from 'package:stats':
## 
##     filter
## The following object is masked from 'package:graphics':
## 
##     layout
msleep
cat(factor(msleep$order))
## 3 15 17 19 2 14 3 17 3 2 2 17 15 17 19 17 19 6 10 7 16 5 13 13 9 15 17 3 15 2 4 3 10 15 15 16 7 15 17 17 17 17 5 17 15 17 17 11 2 15 3 3 3 15 9 15 17 8 3 4 8 6 10 17 17 15 19 17 17 17 17 17 19 2 12 17 13 1 18 4 3 3 3

Pie-Chart

df_order = data.frame(table(msleep$order))
print(df_order)
##               Var1 Freq
## 1     Afrosoricida    1
## 2     Artiodactyla    6
## 3        Carnivora   12
## 4          Cetacea    3
## 5       Chiroptera    2
## 6        Cingulata    2
## 7  Didelphimorphia    2
## 8    Diprotodontia    2
## 9   Erinaceomorpha    2
## 10      Hyracoidea    3
## 11      Lagomorpha    1
## 12     Monotremata    1
## 13  Perissodactyla    3
## 14          Pilosa    1
## 15        Primates   12
## 16     Proboscidea    2
## 17        Rodentia   22
## 18      Scandentia    1
## 19    Soricomorpha    5
fig_order = plot_ly(type='pie', labels=df_order$Var1, values=df_order$Freq, 
                    textinfo='label+percent',insidetextorientation='radial')
fig_order
df_vore = data.frame(table(msleep$vore))
df_vore
fig_vore = plot_ly(type='pie', labels=df_vore$Var1, values=df_vore$Freq,
                   textinfo='label+percent',insidetextorientation='radial')
fig_vore

Scatter plot

fig_sp = plot_ly(data = msleep, x=~brainwt, y=~bodywt, color = ~order)
fig_sp
## No trace type specified:
##   Based on info supplied, a 'scatter' trace seems appropriate.
##   Read more about this trace type -> https://plotly.com/r/reference/#scatter
## No scatter mode specifed:
##   Setting the mode to markers
##   Read more about this attribute -> https://plotly.com/r/reference/#scatter-mode
## Warning: Ignoring 27 observations
## Warning in RColorBrewer::brewer.pal(N, "Set2"): n too large, allowed maximum for palette Set2 is 8
## Returning the palette you asked for with that many colors

## Warning in RColorBrewer::brewer.pal(N, "Set2"): n too large, allowed maximum for palette Set2 is 8
## Returning the palette you asked for with that many colors

Using Normalization

plot(sqrt(msleep$bodywt), sqrt(msleep$brainwt), col="red")
text(sqrt(msleep$bodywt), sqrt(msleep$brainwt), msleep$name, cex=0.5)

plot(msleep$bodywt^(1/100), msleep$brainwt^(1/100), col="red")
text(msleep$bodywt^(1/100), msleep$brainwt^(1/100), msleep$name, cex=0.5)

Scatter plot

plot(log10(msleep$bodywt), log10(msleep$brainwt), col="red")
text(log10(msleep$bodywt), log10(msleep$brainwt), msleep$name, cex=0.5)

t <- list(family = "Helvetica",size = 14,color = "blue")
t1 <- list(family = "Times New Roman",color = "red")
t2 <- list(family = "Courier New",size = 14,color = "green")
t3 <- list(family = 'Arial')
fig_sp = plot_ly(data = msleep, x = ~log10(bodywt), y = ~log10(brainwt), color = ~name,
                  type = 'scatter', mode = 'markers')%>%
                          layout(title= list(text = "Body weight vs Brain weight",font = t1), font=t, 
                          legend = list(title=list(text='Animals',font = t2)), 
                          xaxis  = list(title = list(text ='Brain Weight', font = t3)),
                          yaxis  = list(title = list(text ='Body Weight', font = t3)),
                          plot_bgcolor='#e5ecf6')

fig_sp
## Warning: Ignoring 27 observations
## Warning in RColorBrewer::brewer.pal(N, "Set2"): n too large, allowed maximum for palette Set2 is 8
## Returning the palette you asked for with that many colors

## Warning in RColorBrewer::brewer.pal(N, "Set2"): n too large, allowed maximum for palette Set2 is 8
## Returning the palette you asked for with that many colors

This is shows a clear linear relation-ship between Body weight and Brain weight

Barplot

# Define labels for the bars
labs = c('herbi'='Herbivore',
         'carni'='Carnivore',
         'omni'='Omnivore',
         'insecti'='Insectivore')

bar_plot = ggplot(data=msleep, aes(x = vore, y = ..count.. / sum(..count..),fill = factor(vore))) + 
           geom_bar(color='black') +
           labs(x = "Vore", y = "Percentage of Vore", title  = "Percentage of the quality of the Vore") +
           scale_x_discrete(labels =labs)
           scale_y_continuous(labels = scales::percent)
## <ScaleContinuousPosition>
##  Range:  
##  Limits:    0 --    1
ggplotly(bar_plot)

Histogram

histogram_plot = ggplot(data=msleep, aes(x = sleep_total)) + 
                 geom_histogram(binwidth = 1.25, color = "black",fill = "grey") +
                 labs(x = "Total Time asleep per day(h)", y="Count", title="Count of Total Time asleep per day(h)") +
                 scale_x_discrete(labels =labs)

ggplotly(histogram_plot)

Kernel Density Plot

density_plot = ggplot(data=msleep, aes(x =log10(brainwt))) +
               geom_density(fill = "indianred3") + 
               labs(x = "brain weight", y="density", title="Kernal density of the brain weight")

ggplotly(density_plot)
## Warning: Removed 27 rows containing non-finite values (stat_density).